/*
	Anonymous Image Board Image Downloader - program for downloading pictures from AIB
	Copyright (C) 2011-2012 Metallhead <Metallhead@megabox.ru>

	HomePage:    http://code.google.com/p/aibid/

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "adddialog.h"
#include "ui_adddialog.h"
#include <QSettings>
#include <QFileDialog>
#include <QDir>

#include <QDebug>
addDialog::addDialog(QWidget *parent, QString *siteName, QString *sitePath, int *updateTime, int *threadsAmount, imageTypes * alowedTypes) :
	QDialog(parent),
	ui(new Ui::addDialog),
	site(siteName),
	path(sitePath),
	upTime(updateTime),
	threadsNum(threadsAmount),
	imagesTypes(alowedTypes)
{
	QSettings settings(QCoreApplication::organizationName(),QCoreApplication::applicationName());
	ui->setupUi(this);
	ui->path->setText(QDir::toNativeSeparators(settings.value("settings/path").toString() + "/"));
	tempPath = ui->path->text();
	ui->buttonBox->buttons().at(0)->setEnabled(false);
	ui->updateTime->setText(settings.value("settings/time").toString());
	ui->threadsNum->setText(settings.value("settings/threadsLimit").toString());

}

addDialog::~addDialog()
{
    delete ui;
}

void addDialog::on_setPath_clicked()
{
		QSettings settings(QCoreApplication::organizationName(),QCoreApplication::applicationName());
    ui->path->setText(QFileDialog::getExistingDirectory(this, tr("Open Directory"),ui->path->text(), QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks));
    if(ui->path->text()=="")
        ui->path->setText(tempPath);
    else	
		tempPath = QDir::toNativeSeparators(ui->path->text() + "/");
    ui->path->setText(tempPath+ui->folderName->text());

}

void addDialog::on_buttonBox_accepted()
{

	QDir * dir = new QDir(ui->path->text());
	if(!(dir->exists()))
		dir->mkpath(ui->path->text());

	*site = ui->thread->text();
	*path = ui->path->text();
	*upTime = ui->updateTime->text().toInt();
	*threadsNum = ui->threadsNum->text().toInt();
	imagesTypes->gif = ui->gif->isChecked();
	imagesTypes->png = ui->png->isChecked();
	imagesTypes->jpg = ui->jpg->isChecked();
}

void addDialog::on_folderName_textChanged(QString )
{
	QRegExp notAlowed("([:*?<>\\|\"])");
	bool wrong = false;
	QString userPath = ui->folderName->text();
	if(userPath.contains(notAlowed))
	{
		userPath.remove(notAlowed);
		wrong = true;
	}

	for(int i=0;i<userPath.count()-1;)
	{
		if(QDir::toNativeSeparators(userPath).mid(i,2)==QDir::toNativeSeparators("//"))
		{
			wrong = true;
			userPath.remove(i,1);
		}
		else
			i++;
    }
	if(wrong)
	{
		ui->folderName->setText(userPath);
                return;
	}

	userPath = QDir::toNativeSeparators(userPath);

	if(userPath.startsWith(QDir::separator()))
		userPath.remove(0,1);
	if(userPath.endsWith(QDir::separator()))
		userPath.remove(userPath.count()-1,1);
	if(userPath.isEmpty())
		ui->path->setText(tempPath + userPath);
	else
		ui->path->setText(tempPath + userPath + QString(QDir::separator()));
}


void addDialog::on_thread_textChanged(QString)
{
	checkInput();
}

void addDialog::checkInput()
{
	QRegExp address("\\b(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]*[-A-Za-z0-9+&@#/%=~_|]");
	QRegExp updateTime("[0-9]+");
	QRegExp threadsCount("[0-9]+");
	ui->buttonBox->buttons().at(0)->setEnabled(updateTime.exactMatch(ui->updateTime->text())&&threadsCount.exactMatch(ui->threadsNum->text())&&address.exactMatch(ui->thread->text()));
}

void addDialog::on_updateTime_textChanged(QString)
{
     checkInput();
}

void addDialog::on_threadsNum_textChanged(QString)
{
    checkInput();
}
